<?php

namespace App\Http\Middleware;

use App\Models\WebConfig;
use Closure;
use Illuminate\Support\Facades\Request;

class CheckIp
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $ip = Request::ip();
        $ips_str = WebConfig::getConfig('limit_ips');

        if ($ips_str) {
            $ips_arr = array_filter(explode(',', $ips_str));
            if (in_array($ip, $ips_arr)) {
                return redirect('404');
            }
        }

        return $next($request);
    }
}
